﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace BUS
{
    public class OurRSA
    {
       

        public string PrivateKeyXML
        { get; set; }

        public string PublicKeyXML
        {
            get;
            set;
        }

        public OurRSA(int keysize)
        {
            GenerateNewRSAParams(keysize);
        }
       
        private void GenerateNewRSAParams(int keysize)
        {

            RSACryptoServiceProvider rsa =
                new RSACryptoServiceProvider(keysize);

            this.PrivateKeyXML=
               rsa.ToXmlString(true);
          
            this.PublicKeyXML=
               rsa.ToXmlString(false);
      

        }

       
        //public void Encrypt(string filePublicOnlyKey, string fileSource, string fileEn)
        //{
        //    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

        //    FileStream fsInput = new FileStream(fileSource, FileMode.Open, FileAccess.Read);
        //    FileStream fsOutput = new FileStream(fileEn, FileMode.Create, FileAccess.Write);
        //    //public only RSA parameters for encrypt
        //    StreamReader reader = new StreamReader(filePublicOnlyKey);

        //    string publicOnlyKeyXML = reader.ReadToEnd();
        //    rsa.FromXmlString(publicOnlyKeyXML);
        //    reader.Close();

        //    //read plaintext, encrypt it to ciphertext

        //    byte[] byteInput = new byte[8];
        //    int count = 0;
        //    do
        //    {
        //        count = fsInput.Read(byteInput, 0, 8);
        //        byte[] byteEnc;
        //        if (count <= 0)
        //        {
        //            break;
        //        }
        //        byteEnc = rsa.Encrypt(byteInput, false);
        //        fsOutput.Write(byteEnc, 0, byteEnc.Length);
        //    } while (true);
        //    fsInput.Close();
        //    fsOutput.Close();

        //}

        //public void Decrypt(string filePublicPrivateKey, string fileEn, string fileDe, int keysize)
        //{
        //    RSACryptoServiceProvider rsa =
        //    new RSACryptoServiceProvider();
        //    FileStream fsInput = new FileStream(fileEn, FileMode.Open, FileAccess.Read);
        //    FileStream fsOutput = new FileStream(fileDe, FileMode.Create, FileAccess.Write);
        //    //public and private RSA parameters for encrypt
        //    StreamReader reader =
        //       new StreamReader(filePublicPrivateKey);
        //    string publicPrivateKeyXML = reader.ReadToEnd();
        //    rsa.FromXmlString(publicPrivateKeyXML);
        //    reader.Close();
        //    byte[] byteInput = new byte[keysize / 8];
        //    int count = 0;
        //    do
        //    {
        //        count = fsInput.Read(byteInput, 0, keysize / 8);
        //        byte[] byteDec;
        //        if (count <= 0)
        //        {
        //            break;
        //        }
        //        byteDec = rsa.Decrypt(byteInput, false);
        //        fsOutput.Write(byteDec, 0, byteDec.Length);
        //    } while (true);
        //    fsInput.Close();
            
        //    fsOutput.Close();


        //    //read ciphertext, decrypt it to plaintext
          
        //}
    }
}
